package com.nenu.bdms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nenu.bdms.config.webresult.WebResult;
import com.nenu.bdms.dao.*;
import com.nenu.bdms.module.experiment.Experiment;
import com.nenu.bdms.module.f_e_relation.FundExperimentRelation;
import com.nenu.bdms.module.fund.Fund;
import com.nenu.bdms.module.fund.FundVO;
import com.nenu.bdms.module.t_e_relation.TesteeExperimentRelation;
import com.nenu.bdms.module.test.Test;
import com.nenu.bdms.module.testee.Testee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GetConfigService {

    @Autowired
    private TestMapper testMapper;
    @Autowired
    private FundMapper fundMapper;
    @Autowired
    private TesteeMapper testeeMapper;
    @Autowired
    private ExperimentMapper experimentMapper;
    @Autowired
    private TesteeExperimentRelationMapper testeeExperimentRelationMapper;
    @Autowired
    private FundExperimentRelationMapper fundExperimentRelation;

    /**
     * @description:getTestIds
     * @author:LinCat
     * @return
     */
    public WebResult getTestId() {
        List<Test> testArrayList = testMapper.selectList(new QueryWrapper<Test>());
        for (Test test :testArrayList){
            System.out.println(test);
        }
        return WebResult.okWebResult(testArrayList);
    }

    public WebResult getAllData(Integer testeeId,String seatSno) {
        //第一个查询
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("testee_id",testeeId);
        queryWrapper.eq("seat_sno",seatSno);
        TesteeExperimentRelation testeeExperimentRelation = testeeExperimentRelationMapper.selectOne(queryWrapper);

        if (testeeExperimentRelation==null) return WebResult.failWebResult(-1);
        else {
            //第二个查询
            Testee testee = testeeMapper.selectById(testeeId);
            String testeeName = testee.getTesteeName();
            Integer experimentId = testeeExperimentRelation.getExperimentId();
            //第三个查询
            Experiment experiment = experimentMapper.selectById(experimentId);
            Double initAsset = experiment.getInitAsset();
            Integer experimentDecisionMakingCount = experiment.getExperimentDecisionMakingCount();
            Integer subExperimentCount = experiment.getSubExperimentCount();
            Double exchangeRate = experiment.getExchangeRate();
            //第四个查询
            QueryWrapper queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("experiment_id",experimentId);
            List <FundExperimentRelation> relations = fundExperimentRelation.selectList(queryWrapper2);
            List <FundVO> fundVOS = new ArrayList<>();
            for (FundExperimentRelation fundExperimentRelation:relations
            ) {
                FundVO fundVO = new FundVO();
                fundVO.setRiseProbably(fundExperimentRelation.getRiseRiseProbably());
                fundVO.setRise(fundExperimentRelation.getRise());
                fundVO.setDown(fundExperimentRelation.getDown());
                ArrayList<Double> allocationRate = new ArrayList<>();
                allocationRate.add((double) 0);
                fundVO.setAllocationRate(allocationRate);
                ArrayList<Double> price = new ArrayList<>();
                price.add(fundExperimentRelation.getPrice());
                fundVO.setPrice(price);
                fundVO.setId(fundExperimentRelation.getFundId());
                //第五个查询
                Fund fund = fundMapper.selectById(fundExperimentRelation.getFundId());
                fundVO.setFundName(fund.getFundName());
                fundVO.setFundManager(fund.getFundManager());
                fundVOS.add(fundVO);
            }


            ArrayList currentProfit = new ArrayList<Double>();

            Map<Object, Object> data = new HashMap<Object, Object>();
            data.put("experiment_id",experimentId);
            data.put("testee_id", testeeId);
            data.put("testee_name", testeeName);
            data.put("seat_sno", seatSno);
            data.put("init_asset", initAsset);
            data.put("experiment_decision_making_count", experimentDecisionMakingCount);
            data.put("sub_experiment_count", subExperimentCount);
            data.put("experiment_least_time", experiment.getExperimentLeastTime());
            data.put("exchange_rate", exchangeRate);
            data.put("calculated_asset", initAsset);
            data.put("accumulated_profit", 0.0);
            data.put("current_profit", currentProfit);
            data.put("fund", fundVOS);
            return WebResult.okWebResult(data);
        }


    }
}
